package com.atguigu.day02;


import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 *
 */
public class Example9 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        DataStreamSource<Integer> stream = env.fromElements(1, 2, 3, 4, 5);
        stream.map(new RichMapFunction<Integer, Integer>() {
            @Override
            public void open(Configuration parameters) throws Exception {
                super.open(parameters);
                System.out.println("声明周期开始");
            }

            @Override
            public Integer map(Integer integer) throws Exception {
                System.out.println("卡槽索引："+getRuntimeContext().getIndexOfThisSubtask());
                return integer*integer;
            }

            @Override
            public void close() throws Exception {
                super.close();
                System.out.println("声明周期结束");
            }
        }).setParallelism(2).print();
        env.execute();
    }
}
